home *** CD-ROM | disk | FTP | other *** search
- /*
- * DemoSignedObject.c
- * Copyright © 1992-93 Apple Computer Inc. All rights reserved.
- * This sub-class to CSignedObject demonstrates how the Digital
- * Signature Manager may be used to sign and verify individual
- * data objects.
- */
- #include "Demo.h"
- #include "DemoSignedObject.h"
- #include <CDataFile.h>
- #include <Global.h>
- #include <Exceptions.h>
- #include <CError.h>
- #include <TBUtilities.h>
- #include <OCEErrors.h>
- extern CError *gError;
- extern CursHandle gWatchCursor;
- extern OSType gSignature;
-
- void Message(
- const StringPtr textString
- );
-
-
- void
- DemoSignedObject::IDemoSignedObject(void)
- {
- inherited::ISignedObject();
- itsCheckBoxValue = kExquisiteMask;
- itsRadioButtonValue = 1;
- CopyPString("\pIt’s actually very nice.", itsCommentaryText);
- itsSignatureDataFile = NULL;
- }
-
- unsigned short
- DemoSignedObject::GetCheckBoxValues(void)
- {
- return (itsCheckBoxValue);
- }
-
- void
- DemoSignedObject::SetCheckBoxValues(
- unsigned short newCheckBoxValues
- )
- {
- itsCheckBoxValue = newCheckBoxValues;
- }
-
- unsigned short
- DemoSignedObject::GetRadioButtonValue(void)
- {
- return (itsRadioButtonValue);
- }
-
- void
- DemoSignedObject::SetRadioButtonValue(
- unsigned short newRadioButtonValue
- )
- {
- itsRadioButtonValue = newRadioButtonValue;
- }
-
- void
- DemoSignedObject::GetCommentaryText(
- StringPtr resultText
- )
- {
- CopyPString(itsCommentaryText, resultText);
- }
-
- void
- DemoSignedObject::SetCommentaryText(
- const StringPtr newText
- )
- {
- CopyPString(newText, itsCommentaryText);
- }
-
- /*
- * Sign away. We keep the signature context around
- * for the entire modal dialog operation.
- */
- Boolean
- DemoSignedObject::SignThisObject(void)
- {
- Boolean signSuccessful;
-
- signSuccessful = FALSE;
- TRY {
- SignPrepare(NULL, "\p");
- ProcessObject();
- Sign(gSIGStatusProc);
- signSuccessful = TRUE;
- }
- CATCH {
- NO_PROPAGATE;
- switch (gLastError) {
- case userCanceledErr:
- Message("\pUser cancelled signing");
- break;
- case kSIGPasswordErr:
- Message("\pPassword incorrect");
- break;
- case kSIGSignerErr:
- Message("\pSigner error");
- break;
- case kSIGSignerNotValidErr:
- Message("\pSigner invalid or expired");
- break;
- default:
- gError->CheckOSError(gLastError);
- break;
- }
- }
- ENDTRY;
- /*
- * If we succeed, there's a signature in itsSignature
- * and a pending context.
- */
- return (signSuccessful);
- }
-
- Boolean
- DemoSignedObject::VerifyThisObject(void)
- {
- Boolean verifySuccessful;
-
- verifySuccessful = FALSE;
- TRY {
- CheckForSignature();
- VerifyPrepare(gSIGStatusProc);
- ProcessObject();
- Verify();
- verifySuccessful = TRUE;
- }
- CATCH {
- NO_PROPAGATE;
- switch (gLastError) {
- case kSIGNoSignature:
- Message(
- "\pYou must create or read"
- " a signature first."
- );
- break;
- case userCanceledErr:
- Message("\pUser cancelled verification");
- break;
- case kSIGSignerErr:
- Message("\pThe signature is not valid");
- break;
- case kSIGVerifyFailedErr:
- Message("\pVerify failed");
- break;
- case kSIGInvalidCredentialErr:
- Message(
- "\pVerify succeeded, but credentials"
- " are out of date or unverified."
- );
- verifySuccessful = TRUE;
- break;
- default:
- gError->CheckOSError(gLastError);
- break;
- }
- }
- ENDTRY;
- if (verifySuccessful)
- ShowSigner("\p");
- /*
- * We don't dispose of the context so that the
- * user can choose "Show Signer."
- */
- return (verifySuccessful);
- }
-
- void
- DemoSignedObject::ProcessObject(void)
- {
- /*
- * Note: ProcessData does not move or purge memory,
- * so we don't need to lock down the object.
- */
- ProcessData(&itsCheckBoxValue, sizeof itsCheckBoxValue);
- ProcessData(&itsRadioButtonValue, sizeof itsRadioButtonValue);
- ProcessData(&itsCommentaryText[1], itsCommentaryText[0]);
- }
-
- /*
- * This is a straight-forward method that writes the signature
- * to a file. In a real-world situation, you would typically
- * also write the dialog (or document) contents.
- */
- void
- DemoSignedObject::SaveObjectSignature(void)
- {
- CDataFile *aDataFile;
- SFReply macSFReply;
- Point corner;
-
- CheckForSignature();
- FindDlogPosition('DLOG', putDlgID, &corner);
- SFPutFile(
- corner,
- "\pSave Signature as:",
- "\pUntitled Signature",
- NULL,
- &macSFReply
- );
- if (macSFReply.good) {
- aDataFile = new (CDataFile);
- TRY {
- aDataFile->IDataFile();
- aDataFile->SFSpecify(&macSFReply);
- aDataFile->CreateNew(gSignature, kSignatureFileType);
- aDataFile->Open(fsRdWrPerm);
- WriteSignature(aDataFile);
- aDataFile->Close();
- }
- CATCH {
- ForgetObject(aDataFile);
- }
- ENDTRY;
- ForgetObject(aDataFile);
- }
- }
-
- /*
- * This method reads a signature from a file created by
- * SaveSignature.
- */
- void
- DemoSignedObject::ReadObjectSignature(void)
- {
- CDataFile *aDataFile;
- SFReply macSFReply;
- Point corner;
- SFTypeList sigTypeList;
-
- sigTypeList[0] = kSignatureFileType;
- FindDlogPosition('DLOG', getDlgID, &corner);
- SFGetFile(corner, NULL, NULL, 1, sigTypeList, NULL, &macSFReply);
- if (macSFReply.good) {
- aDataFile = new (CDataFile);
- TRY {
- aDataFile->IDataFile();
- aDataFile->SFSpecify(&macSFReply);
- aDataFile->Open(fsRdWrPerm);
- ReadSignature(aDataFile);
- aDataFile->Close();
- }
- CATCH {
- ForgetObject(aDataFile);
- }
- ENDTRY;
- ForgetObject(aDataFile);
- }
- }
-